﻿using MKAdminCore.DTO.CusAttribute;
using MKAdminCore.DTO.Web.Common;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace MKAdminCore.DTO.Web.CusAttribute
{
    public class CusRegexAttribute : AbstractCusAttribute
    {
        /// <summary>
        /// 正则表达式
        /// </summary>
        public string RegexContent { get; set; }

        /// <summary>
        /// 验证是否为空
        /// </summary>
        /// <param name="oValue"></param>
        /// <returns></returns>
        public override HttpResponseResult Validate<T>(T dto, PropertyInfo prop)
        {
            var result = new HttpResponseResult();
            //获取属性内容
            object oValue = prop.GetValue(dto);

            var r_content = new Regex(this.RegexContent, RegexOptions.IgnoreCase);
            Match m_content = r_content.Match(oValue.ToString());

            result.Code = m_content.Success ? 200 : 400;
            if (result.Code != 200)
            {
                result.Msg = base.ErrorMsg;
            }

            return result;
        }
    }
}
